import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('@/views/Home.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue')
  },
  {
    path: '/duorou',
    name: 'duorou',
    component: () => import('@/views/duorou.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由前置拦截，作用是：当用户尚未登录时不允许访问其他页面，登录接口调试完成后应打开下方注释代码
// router.beforeEach((to, from, next) => {
//   const user = JSON.parse(localStorage.getItem('user'))
//   if (to.name !== 'Login') {
//     if (!user || !user.id) {
//       next('/login')
//     } else {
//       next()
//     }
//   } else {
//     next()
//   }
// })

export default router
